﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_UserManager_EditDepartment : PowerCreator.LMS.Web.Base.BasePage
{
    Department opDepart = new Department();
    User opUser = new User();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.UpdateDepartment))
        {
            Script.RedirectURL(GetRefUrl());
        }

        if (!IsPostBack)
        {
            InitPage();

            string department = Request.QueryString["DepartmentID"].ToString();
            ViewState["DepartmentID"] = department;

            BinderDDL();
            
            //初始化
            VOTDepartment vo = opDepart.Load(int.Parse(department));
            ddl_Department.SelectedValue = vo.DepartmentFatherId.ToString();
            txt_DepartmentName.Text = vo.DepartmentName;
            txt_Description.Text = vo.DepartmentContent;
            if (vo.DepartmentType == 2)
            {
                rdbtnTypeClass.Checked = true;
                rdbtnTypeDepartment.Checked = false;
                ddl_Department.Enabled = false;
            }
            else if (vo.DepartmentType == 1)
            {
                rdbtnTypeDepartment.Checked = true;
                rdbtnTypeClass.Checked = false;
            }
        }
    }
    //绑定部门信息
    private void BinderDDL()
    {
        string search = string.Format("DepartmentID <> {0}", ViewState["DepartmentID"]);
        DataTable dt = opDepart.Query(search);
        ArrayList list = new ArrayList();
        list = opDepart.GetDepartmentTree(dt);
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("department_defaultdeparmentnameroot", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        list.Insert(0, li);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_EditDepartment.Text = GetLanageValueByKey("lbl_EditDepartment_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditDepartment_Name.Text = GetLanageValueByKey("lbl_EditDepartment_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditDepartment_higherUp.Text = GetLanageValueByKey("lbl_AddDpt_Updep", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditDepartment_Remark.Text = GetLanageValueByKey("lbl_EditDepartment_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Sort.Text = GetLanageValueByKey("lbl_EditDepartment_Sort", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtnTypeDepartment.Text = GetLanageValueByKey("department_departmenttype1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rdbtnTypeClass.Text = GetLanageValueByKey("department_departmenttype2", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private bool CheckPageData()
    {
        string search = string.Format("DepartmentName='{0}' and DepartmentID <> {1}", txt_DepartmentName.Text, ViewState["DepartmentID"]);
        if (opDepart.Count(search) != 0)
        {
            lbl_msg_departmentname.Text = GetLanageValueByKey("lbl_AddDepartment_departmentName", PowerCreator.LMS.Language.LanagetItemType.MSG);
            return false;
        }
        if (rdbtnTypeClass.Checked)
        {
            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                VOTDepartment vot = opDepart.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
                if (vot.DepartmentType == 2)
                {
                    lbl_msg_departmentname.Text = GetLanageValueByKey("lbl_Editorpartment_departdepartmenttype", PowerCreator.LMS.Language.LanagetItemType.MSG);
                    return false;
                }
            }
        }
        
        return true;
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        VOTDepartment vo = opDepart.Load(int.Parse(ViewState["DepartmentID"].ToString()));
        vo.DepartmentName = txt_DepartmentName.Text;
        vo.DepartmentContent = txt_Description.Text;
        if (rdbtnTypeClass.Checked)
        {
            vo.DepartmentType = 2;
        }
        else {
            vo.DepartmentType = 1;
        }
        vo.DepartmentFatherId = int.Parse(ddl_Department.SelectedValue);
        if (opDepart.Update(vo))
        {
            string url ="Department.aspx";
            //if (vo.DepartmentType == 2)
            //{
            //    url = "SetDepartmentAttributesEdit.aspx?DepartmentID=" + vo.DepartmentId.ToString();
            //}
 
            Script.RedirectURL(url);
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Department.aspx";
        Script.RedirectURL(url);
    }
    protected void rdbtnTypeDepartment_CheckedChanged(object sender, EventArgs e)
    {
        VOTDepartment vo = opDepart.Load(int.Parse(ViewState["DepartmentID"].ToString()));
        if (vo.DepartmentType == 1)
        {
            string search = string.Format("FKTeacherDepartmentID={0}", ViewState["DepartmentID"]);
            if (opUser.QueryTeacherCount(search) != 0)
            {
                lbl_msg_departmentname.Text = GetLanageValueByKey("lbl_Editorpartment_departdepartmentcountteacher", PowerCreator.LMS.Language.LanagetItemType.MSG);
                rdbtnTypeDepartment.Checked = true;
                rdbtnTypeClass.Checked = false;
            }
        }
        else if (vo.DepartmentType == 2)
        {
            string search = string.Format("FKStudentDepartmentID={0}", ViewState["DepartmentID"]);
            if (opUser.QueryStudentCount(search) != 0)
            {
                lbl_msg_departmentname.Text = GetLanageValueByKey("lbl_Editorpartment_departdepartmentcountteacher", PowerCreator.LMS.Language.LanagetItemType.MSG);
                rdbtnTypeDepartment.Checked = false;
                rdbtnTypeClass.Checked = true;
            }
        }
        
    }
    protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        VOTDepartment vo = opDepart.Load(int.Parse(ViewState["DepartmentID"].ToString()));
        VOTDepartment vop = opDepart.Load(vo.DepartmentFatherId);
        if (ddl_Department.SelectedValue.ToString() != "0")
        {
            VOTDepartment vot = opDepart.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
            if (vot.DepartmentType == 2)
            {
                lbl_msg_departmentname.Text = "不能修改部门上一级，所选部门上一级为班级类型";
                ddl_Department.SelectedValue = vo.DepartmentFatherId.ToString();
                return;
            }
        }

    }
}
